<?php

namespace app\admin\controller;

use app\common\model\admin\AdminRole;
use app\common\model\admin\AdminUser;
use think\Db;
use think\facade\Session;
use app\common\controller\Base;

class User extends Base
{
    // 登录页面
    public function loginView()
    {
        return $this->fetch('user/login');
    }

    // 登录接口
    public function login()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');

        $adminUser = AdminUser::get(['username' => $username, 'password' => md5($password)]);

        if ($adminUser) {
            Session::set('adminUser', $adminUser);
            $this->success('ok');
        } else {
            $this->error('fail');
        }
    }

    // 登出接口
    public function loginOut()
    {
        Session::delete('adminUser');
        $this->success('ok');
    }

    // 管理员列表页面
    public function adminUserList()
    {
        return $this->fetch('user/adminUserList');
    }

    // 获取所有管理员
    public function getUserList()
    {
        $keyword = $this->request->post('keyword', '');
        $list = AdminUser::whereLike('nickname|department', '%' . $keyword . '%')->select();

        if ($list) {
            foreach ($list as $key => $value) {
                $roles = AdminRole::where('id', 'in', $value->roleid)->select();
                if ($roles) {
                    $str = '';
                    foreach ($roles as $role) {
                        $str = $str . ',' . $role->rolename;
                    }
                    $str = ltrim($str, ',');
                    $value->roleid = $str;
                }
            }
            $this->result('ok', $list);
        }
        $this->result('fail');
    }

    public function deleteUser()
    {
        $uid = $this->request->post('uid');
        $data = [
            'delstatus' => 4,
            'updatetime' => date('Y-m-d H:i:s', time())
        ];
        $res = Db::name('nx_admin_user')->where('id', 'in', $uid)->update($data);
        if ($res) {
            return $this->api_resp_success('', '', $res);
        }
    }

    public function addUser()
    {
        return $this->fetch('user/addUser');
    }

    public function doAddUser()
    {
        $username = $this->request->post('username');
        $name = $this->request->post('name');
        $pwd = $this->request->post('pwd');
        $department = $this->request->post('department');
        $data = [
            'loginid' => $name,
            'pic' => 'http://wechat.nanxuncn.cn/static/dist/img/user2-160x160.jpg',
            'username' => $username,
            'password' => md5($pwd),
            'department' => $department,
            'createtime' => date('Y-m-d H:i:s', time())
        ];
        $res = Db::name('nx_admin_user')->insert($data);
        if ($res) {
            return $this->api_resp_success('', '', $res);
        }
    }

    public function editUser()
    {
        $id = $this->request->param('id');
        $data = Db::table('nx_admin_user')->where('id', $id)->find();
        $this->assign('data', $data);
        return $this->fetch('user/editUser');
    }

    public function doEditUser()
    {
        $parame = $this->request->param();
        if ($parame['pwd'] != '') {
            $data = [
                'loginid' => $parame['loginid'],
                'username' => $parame['username'],
                'password' => md5($parame['pwd']),
                'department' => ['department'],
                'updatetime' => date('Y-m-d H:i:s', time())
            ];
        } else {
            $data = [
                'loginid' => $parame['loginid'],
                'username' => $parame['username'],
                'department' => $parame['department'],
                'updatetime' => date('Y-m-d H:i:s', time())
            ];
        }
        $res = Db::name('nx_admin_user')->where('id', $parame['id'])->update($data);
        return $this->api_resp_success('', '', $res);
    }

    public function addRoleList()
    {
        $id = $this->request->param('id');
        $list = Db::table('nx_admin_role')->where('delstatus', '1')->select();
        $this->assign('list', $list);
        $this->assign('id', $id);
        return $this->fetch('user/addRoleList');
    }

    public function doAddrole()
    {
        $ids = $this->request->post('ids');
        $id = $this->request->post('id');
        $data = [
            'roleid' => implode(',', $ids),
            'updatetime' => date('Y-m-d H:i:s', time())
        ];
        $res = Db::name('nx_admin_user')->where('id', $id)->update($data);
        if ($res) {
            return $this->api_resp_success('', '', $res);
        }
    }

}
